package com.longrise.android.byjk.plugins.dealsituation.course.video2.education.picedu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.education.picedu.auxiliary.CurrentViewPager;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoConsts;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.android.byjk.widget.view.BaseFrameView;


/**
 * Created by godliness on 2017/7/31.
 * 学历教育之图片课件学习方式
 */

public class PictureActivity extends BaseActivity<PicPrecenter> implements PicView, BaseFrameView.ReloadListener, View.OnClickListener {

    private static final String TAG = "PictureActivity";

    private final String SAVE_PIC_POSITION = "SAVE_PIC_POSITION";

    private Toolbar mToolbar;
    private TextView mTvRight;

    private CurrentViewPager mViewPager;
    private PicAdapter mPicAdapter;
    private BaseFrameView mBaseFrameView;

    private int mCurrentPosition = 0;

    private VideoParams mParams;

    private View mGestureView;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mParams = savedInstanceState.getParcelable(VideoConsts.VIDEO_PARAMS_PARCELABLE);
        } else {
            getExtraData();
        }
        return R.layout.activity_picture;
    }

    @Override
    public void initView() {
        initTitle();
        mViewPager = (CurrentViewPager) findViewById(R.id.vp_picact);
        mBaseFrameView = (BaseFrameView) findViewById(R.id.bfv_pic);
        setPicAdapter();

        initGestureView();

        preformLoad();
    }

    private void initGestureView() {
        mGestureView = findViewById(R.id.gesture_rootview);
        mGestureView.setVisibility(View.GONE);
    }

    private void initTitle() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTvRight = (TextView) findViewById(R.id.iv_toolbar_right_tv);
        mTvRight.setTextColor(getResources().getColor(R.color.swipe_refresh_color1));
        RelativeLayout rightRl = (RelativeLayout) findViewById(R.id.bb_toolbar_right_rl_tv);
        rightRl.setVisibility(View.VISIBLE);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void beforeSetContentView() {
        initTheme();
        screenToFull();
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        orientationToLandscape();
    }

    private void getExtraData() {
        Intent extra = getIntent();
        mParams = extra.getParcelableExtra(VideoConsts.VIDEO_PARAMS_PARCELABLE);
    }

    /**
     * 加载资源
     */
    private void preformLoad() {
        mPresenter.reLoad();
    }

    private void setPicAdapter() {
        mPicAdapter = new PicAdapter(this);
        mViewPager.setAdapter(mPicAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
                mPresenter.setCurrentPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mBaseFrameView.setOnReloadListener(this);
    }

    @Override
    public void parse(EntityBean[] result) {
        if (mPicAdapter != null) {
            mPicAdapter.setData(result);
            mPicAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {

    }

    private void showGestureView() {
        boolean isGestured = CacheUtils.getBoolean(TAG, false);
        if (mGestureView != null && !isGestured) {
            mGestureView.setOnClickListener(this);
            mGestureView.setVisibility(View.VISIBLE);
            mGestureView.postDelayed(mDelayRunnable, 3000);
            CacheUtils.setBoolean(TAG, true);
        }
    }

    private void hideGestureView() {
        if (mGestureView != null) {
            mGestureView.setVisibility(View.GONE);
            mGestureView.removeCallbacks(mDelayRunnable);
        }
    }

    private Runnable mDelayRunnable = new Runnable() {
        @Override
        public void run() {
            hideGestureView();
        }
    };

    private void loadStartStatus() {
        mBaseFrameView.setLoadingStatus(BaseFrameView.LoadingStatus.LOADING);
        mBaseFrameView.setVisibility(View.VISIBLE);
    }

    private void loadFinishStatus() {
        mBaseFrameView.setLoadingStatus(BaseFrameView.LoadingStatus.SUCCESS);
        mBaseFrameView.setVisibility(View.GONE);
        showGestureView();
    }

    private void loadErrorStatus() {
        mBaseFrameView.setLoadingStatus(BaseFrameView.LoadingStatus.ERROR);
        mBaseFrameView.setVisibility(View.VISIBLE);
    }

    @Override
    public void error() {
        mBaseFrameView.post(new Runnable() {
            @Override
            public void run() {
                loadErrorStatus();
            }
        });
    }

    @Override
    public void setTitle(String title) {
        if (mParams != null) {
            mToolbar.setTitle(mParams.mCwidName);
        }
    }

    @Override
    public void setRightTv(String infor) {
        mTvRight.setText(infor);
    }

    @Override
    public VideoParams getVideoParams() {
        return mParams;
    }

    @Override
    public void onBack() {
        finish();
    }

    @Override
    public boolean actFinished() {
        return isFinishing();
    }

    @Override
    public void showToast(String msg) {
        super.showToast(msg);
    }

    @Override
    public void showLoadingDialog() {
        mBaseFrameView.post(new Runnable() {
            @Override
            public void run() {
                loadStartStatus();
            }
        });
    }

    @Override
    public void dismissLoadingDialog() {
        mBaseFrameView.post(new Runnable() {
            @Override
            public void run() {
                loadFinishStatus();
            }
        });
    }

    @Override
    public void onReload(View v) {
        mPresenter.reLoad();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_PIC_POSITION, mCurrentPosition);
        outState.putParcelable(VideoConsts.VIDEO_PARAMS_PARCELABLE, mParams);
    }

    private Runnable mRecoveryRunnable = new Runnable() {
        @Override
        public void run() {
            mViewPager.setCurrentItem(mCurrentPosition, false);
        }
    };

    @Override
    public void onRestoreInstanceState(final Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(SAVE_PIC_POSITION);
            mViewPager.post(mRecoveryRunnable);
        }
    }

    private void sendMessage() {
        mPresenter.toRefrushCourseDetail();
    }

    private void release() {
        mPicAdapter = null;
        hideGestureView();
    }

    @Override
    protected void onDestroy() {
        release();
        sendMessage();
        super.onDestroy();
    }

}
